package com.javasm.service.redisDictService.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasm.domin.entity.base.SysDict;
import com.javasm.domin.entity.base.SysDictType;
import com.javasm.mapper.redisDictMapper.SysDictMapper;
import com.javasm.mapper.redisDictMapper.SysDictTypeMapper;
import com.javasm.service.redisDictService.SysDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Transactional
@Service
@Slf4j
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper,SysDict> implements SysDictService {
    @Resource
    SysDictMapper sysDictMapper;

    @Resource
    SysDictTypeMapper sysDictTypeMapper;

    @Resource
    RedisTemplate<String,String> redisTemplate;

    public static final LinkedHashMap<String, String> redisCookie = new LinkedHashMap<>();

    public static final Map<String,LinkedHashMap<Integer,String>> dicCache = new HashMap<>();
    /**
     * 转化码值,从redis读取
     *
     * @param distname
     * @param value
     * @return
     * @throws Exception
     */
    @Override
    public String transformStr(String distname, int value) {
        return redisTemplate.opsForValue().get(distname + "_" + value) != null ?
               Objects.requireNonNull(redisTemplate.opsForValue().get(distname + "_" + value)) : String.valueOf(value);
    }

    /**
     * 转化码值,从内存中读取
     */
    @Override
    public String ramTransformStr(String distname, int value) {
        return redisCookie.get(distname+"_"+value);
    }

    /**
     * 字典内存
     * @param distname
     * @return
     */
    @Override
    public Map<String, LinkedHashMap<Integer, String>> transformTypeList(String distname) {
        LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
        for (int i = 0; i < redisCookie.size(); i++) {
            if (redisCookie.get(distname+"_"+i)!=null){
                map.put(i,redisCookie.get(distname+"_"+i));
            }
        }
        HashMap<String, LinkedHashMap<Integer, String>> hashMap = new HashMap<>();
        hashMap.put(distname,map);
        return hashMap;
    }

    /**
     * 获取所有字典数据
     * @return
     */
    @Override
    public Map<String, LinkedHashMap<Integer, String>> transformTypeList() {
        List<SysDictType> dictTypeAll = sysDictTypeMapper.getDictTypeAll();
        LinkedHashMap<Integer, String> map = new LinkedHashMap<>();
        for (int i = 0; i < dictTypeAll.size(); i++) {
            String code = dictTypeAll.get(i).getCode();
            List<SysDict> dicts = sysDictMapper.getByNameAll(code);
            for (int i1 = 0; i1 < dicts.size(); i1++) {
                map.put(i1,dicts.get(i1).getName());
            }
            dicCache.put(code,map);
        }
        return dicCache;
    }


    /**
     * 刷新缓存
     */
    @Override
    public void refreshCache() {
        log.info("start 刷新码表缓存");

        List<SysDict> sysDictPojoList = sysDictMapper.getDictAll();
        long startTime = System.currentTimeMillis();
        for (SysDict sysDictPojo : sysDictPojoList) {
            redisTemplate.opsForValue().set(sysDictPojo.getTypeCode() + "_" + sysDictPojo.getValue(), sysDictPojo.getName());
            redisCookie.put(sysDictPojo.getTypeCode() + "_" + sysDictPojo.getValue(), sysDictPojo.getName());
        }
        System.out.println(redisCookie);
        //getTypeCode color  getValue 1  getName() 蓝 拼接color_1
        long endTime = System.currentTimeMillis();
        log.info("end 刷新码表缓存，总计：" + sysDictPojoList.size() + "条，用时：" + (endTime - startTime) + "毫秒");
    }
}
